package com.danciyixue.boot.client.users.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.dcyx.model.form.DcyxUsersForm;
import com.danciyixue.boot.dcyx.model.query.DcyxUsersQuery;
import com.danciyixue.boot.dcyx.model.vo.DcyxUsersVO;
import com.danciyixue.boot.dcyx.service.DcyxUsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:58
 */
@Tag(name = "05. 用户", description = "用户")
@RestController
@RequestMapping("/api/client/v1/dcyx-users")
@RequiredArgsConstructor
public class ClientDcyxUsersController {

    private final DcyxUsersService dcyxUsersService;

    // 用户查询个人信息
    @Operation(summary = "根据token查询单个用户信息")
    @GetMapping("/me")
    public Result<DcyxUsersVO> getDcyxUsersMe() {
        DcyxUsersVO result = dcyxUsersService.getDcyxUsersMe();
        return Result.success(result);
    }

    @Operation(summary = "用户信息分页列表")
    @GetMapping("/page")
    public PageResult<DcyxUsersVO> getDcyxUsersPage(DcyxUsersQuery queryParams ) {
        IPage<DcyxUsersVO> result = dcyxUsersService.getDcyxUsersPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户信息")
    @PostMapping
    public Result<Void> saveDcyxUsers(@RequestBody @Valid DcyxUsersForm formData ) {
        boolean result = dcyxUsersService.saveDcyxUsers(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户信息表单数据")
    @GetMapping("/{id}/form")
    public Result<DcyxUsersForm> getDcyxUsersForm(
        @Parameter(description = "用户信息ID") @PathVariable Long id
    ) {
        DcyxUsersForm formData = dcyxUsersService.getDcyxUsersFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户信息")
    @PutMapping(value = "/{id}")
    public Result<Void> updateDcyxUsers(
            @Parameter(description = "用户信息ID") @PathVariable Long id,
            @RequestBody @Validated DcyxUsersForm formData
    ) {
        boolean result = dcyxUsersService.updateDcyxUsers(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户信息")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteDcyxUserss(
        @Parameter(description = "用户信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = dcyxUsersService.deleteDcyxUserss(ids);
        return Result.judge(result);
    }

    @Operation(summary = "重置用户密码")
    @PutMapping(value = "/{id}/password/reset")
    public Result<Void> resetPassword(
            @Parameter(description = "用户信息ID") @PathVariable Long id,
            @RequestParam String password
    ) {
        boolean result = dcyxUsersService.resetPassword(id, password);
        return Result.judge(result);
    }
}
